﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample81A
{
    public class SightingSystem : ComponentSystem
    {

        private Vector3 Position = new Vector3(0, 0, 30);
        private Camera Camera;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public SightingSystem()
        {
        }

        public override void Initialization()
        {
            ContextManager.Foreach<SightingGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                IRenderGroup renderGroup = context.GetContextRenderGroup();
                Camera = renderGroup.Camera;
                group.Sighting.Scale = new Vector3(0.85f, 0.85f, 0.85f);
                return group;
            });
        }

        public override void Update(TimeSpan lastStamp)
        {
            if(Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Press)
            {
                Position = Camera.ScreenToWorldPoint(Input.GetMousePosition(), 30);
            }
            ContextManager.Foreach<SightingGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                ref MVPMatrix mvp = ref context.GetMVPMatrix();
                mvp.ModelMatrix = Matrix4x4.CreateTranslation(Position) * Matrix4x4.CreateScale(group.Sighting.Scale);
                context.PushToRenderGroup();
            });
        }
    }
}
